package com.zzy.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import com.zzy.model.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.zzy.util.FreeMarkerUtil;
import org.springframework.web.servlet.ModelAndView;


@Controller
@RequestMapping()
public class TestController {

    /**使用 FreeMarkerUtil 可以 忽略和 spring  的集成 */
    @RequestMapping(value="test",produces = "application/json; charset=utf-8")
    @ResponseBody
    public String testFreeMarker(HttpServletRequest request){

        Map<String,Object> map = new HashMap<String,Object>();
        map.put("name", "【我是名字】");

        return FreeMarkerUtil.instance().geneFileStr(request, "testFreeMarker.flt", map);
    }


    /**使用  spring   集成 */
    @RequestMapping(value="test1",produces = "application/json; charset=utf-8")
    public String testFreeMarker1(HttpServletRequest request){
        ModelAndView model = new ModelAndView();
        String word0 = "Hello ";
        String word1 = "World!";
        //将数据添加到视图数据容器中
        request.setAttribute("word0",word0);
        request.setAttribute("word1",word1);

        /**传递一个实体*/
        User user1 = new User("小明",20);
        request.setAttribute("user",user1);

        /**传递一个list*/
        User user2 = new User("小花",21);
        User user3 = new User("小刚",22);
        List list = new ArrayList();
        list.add(user1);list.add(user2);list.add(user3);
        request.setAttribute("list1",list);
        return "freeMarker/Hello.ftl";
    }
}